const { ipcRenderer } = require('electron')

document.getElementById('user-add').addEventListener('submit', (evt) => {

  evt.preventDefault()
  const user_name = evt.target[0].value
  const user_password = evt.target[1].value
  const user_acces = evt.target[2].value

  let user = new Object();
  user.name = user_name
  user.password = user_password
  user.permission = user_acces
  ipcRenderer.send('user-add', user);

});


function deleteUser(id){
  ipcRenderer.send('user-del', id);
}
ipcRenderer.on('users', (event, users) => {
  const userList = document.getElementById('users-data')

  // create html string
  let html = "";
  users.forEach(function(object, i){
    html += '<tr>\n';
    for (let key in object){
      if (key != 'id'){
        html+= '<td class="user-item">'+object[key]+'</td>';

      }
    }
    html+='<td class="user-item"> <button onclick=deleteUser(this.id) id='+ object.id +' class="btn btn-error">Delete</button>'
    html+= "</tr>\n";

  });
  userList.innerHTML = html;



});
